//
//  MainView.swift
//  weibo
//
//  Created by 杨晨 on 16/3/8.
//  Copyright © 2016年 杨晨. All rights reserved.
//

import UIKit
import SnapKit
//申明协议,需要继承基协议
protocol MainViewDelegate: NSObjectProtocol {
    
    //声明协议的方法
    //注册
    func Register()
    //登陆
    func LogIn()
    
}

class MainView: UIView {

    //声明代理对象 代理对象声明在oc中必须是weak或者assagin
  weak var mainDelegate: MainViewDelegate?
    //    var didClick: (()->())?
    //外部可调用的方法
    func setupInfoWith(title: String, imageName: String?){
        
        myLable.text=title;
        
        if imageName != nil {
            //不是首页
            circleImg.image=UIImage(named: imageName!)
            //隐藏大房子
           bigHourse.hidden = true
            //设置不是渐隐
            bringSubviewToFront(circleImg)
            
       }else{
            //是首页,让圆圈转起来
            
            startrow()
            
    }
        
        
    }
    
    //让圆圈动起来
    private func startrow() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.repeatCount = MAXFLOAT
        //动画时间
        anim.duration = 10
        
        anim.toValue = 2*M_PI
        
        anim.removedOnCompletion = false
        //添加到图层
        circleImg.layer.addAnimation(anim, forKey: nil)
        
    }
    
    //要自定义视图的话首先找到入口
    override init (frame:CGRect){
        super.init(frame:frame)
        
        load()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func load(){
        addSubview(circleImg)
        addSubview(bigHourse)
        addSubview(myLable)
        addSubview(myBtn)
        addSubview(myBtn1)
        addSubview(maskImage)
        circleImg.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        bigHourse.snp_makeConstraints { (make) -> Void in
            
            make.center.equalTo(circleImg.snp_center)
        }
        myLable.snp_makeConstraints { (make) -> Void in
            
            make.width.equalTo(250)
            make.centerX.equalTo(circleImg.snp_centerX)
            make.centerY.equalTo(circleImg.snp_centerY).offset(110)
            
        }
        myBtn.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleImg.snp_centerX).offset(-80)
            make.centerY.equalTo(circleImg.snp_centerY).offset(150)
            make.width.equalTo(100)
        }
        myBtn1.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleImg.snp_centerX).offset(80)
            make.centerY.equalTo(circleImg.snp_centerY).offset(150)
            make.width.equalTo(100)
        }
        maskImage.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(circleImg.snp_centerX)
            make.centerY.equalTo(circleImg.snp_centerY)
        }
        let color: CGFloat = 236.0/255.0
        self.backgroundColor = UIColor(red: color, green: color, blue: color, alpha: 1.0)
//        bigHourse.bringSubviewToFront(bigHourse)
        bringSubviewToFront(bigHourse)
        
        // 给按钮添加点击事件
        myBtn.addTarget(self, action: "myBtnClick", forControlEvents: .TouchUpInside)
        
         myBtn1.addTarget(self, action: "myBtn1Click", forControlEvents: .TouchUpInside)
        
    }
    
    //按钮的监听事件
    //跳转是控制器之间的事情,从一个控制器跳转到另一个控制器,然后在加载view视图,不能直接存一个视图跳转到一个控制器
    
    //登陆
    func myBtnClick(){
        //执行闭包
//        didClick!()
        print("我是登陆")
        mainDelegate?.LogIn()
    }
    
    func myBtn1Click(){
        
        print("我是注册")
        mainDelegate?.Register()
    }
    
    //懒加载 视图
    private lazy var circleImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //懒加载大房子
    private lazy var bigHourse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var maskImage: UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    
    private lazy var myLable: UILabel = {
        
        let l = UILabel()
        //l.text="关注一些人"
        l.textColor = UIColor.grayColor()
        l.font = UIFont.systemFontOfSize(14)
        // 自适应大小
        l.sizeToFit()
        
        l.numberOfLines = 0
        l.textAlignment = .Center
        return l;
        
    }()
    
    private lazy var myBtn: UIButton = {
        
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("登陆", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setTitleColor(UIColor.grayColor(), forState: .Selected)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.sizeToFit()
        
        return btn
    }()
    
    private lazy var myBtn1: UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Selected)
        btn.sizeToFit()
        return btn
        
        
    }()
    
}
